/**
 * 神州信息(2012)
 */
package net.abbp.watchkeeper;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

import net.abbp.core.util.NumberUtils;
import net.abbp.dao.DaoFactory;
import net.abbp.dao.IDao;

/**
 * 联系人管理面板
 * 
 * @version 1.0
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ABBP 1.0
 * 
 *        Date：Apr 8, 2012
 */
public class LinkmanPane extends JPanel {
    private JTable grid;
    private LinkmanTableModel model;

    public LinkmanPane() {
        this.initComponents();
    }

    /**
     * 更新数据
     */
    public void refresh() {
        if (model != null) {
            model.refresh();
            grid.updateUI();
        }
    }

    /**
     * 初始化
     */
    private void initComponents() {
        this.setLayout(new BorderLayout());
        JPanel paneFunc = new JPanel();
        JButton btnFunc;

        btnFunc = new JButton("增加");
        btnFunc.setPreferredSize(new Dimension(80, 32));
        btnFunc.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JLinkmanCard.insert(LinkmanPane.this);
            }
        });
        paneFunc.add(btnFunc);

        btnFunc = new JButton("修改");
        btnFunc.setPreferredSize(new Dimension(80, 32));
        btnFunc.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int keyId = getKeyId();
                if (keyId == -1) {
                    JOptionPane.showMessageDialog(SwingUtilities.getWindowAncestor(LinkmanPane.this), "请选择需要修改的信息",
                            "修改", JOptionPane.WARNING_MESSAGE);

                } else {
                    JLinkmanCard.edit(LinkmanPane.this, keyId);
                }
            }
        });
        paneFunc.add(btnFunc);

        btnFunc = new JButton("删除");
        btnFunc.setPreferredSize(new Dimension(80, 32));
        btnFunc.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                int keyId = getKeyId();
                if (keyId == -1) {
                    JOptionPane.showMessageDialog(SwingUtilities.getWindowAncestor(LinkmanPane.this), "请选择需要删除的信息",
                            "删除", JOptionPane.WARNING_MESSAGE);

                } else {
                    IDao dao = DaoFactory.INSTANCE.newDao();

                    dao.delete(Linkman.class, keyId);

                    LinkmanPane.this.refresh();
                }
            }
        });
        paneFunc.add(btnFunc);

        this.add(paneFunc, BorderLayout.NORTH);
        this.initTable();
        this.add(new JScrollPane(this.grid), BorderLayout.CENTER);
    }

    /**
     * 初始化表格
     */
    private void initTable() {
        this.model = new LinkmanTableModel();
        this.grid = new JTable(this.model);
        this.grid.getTableHeader().setPreferredSize(new Dimension(10000, 24));
        this.grid.setRowHeight(24);

        TableColumn firstColumn = this.grid.getTableHeader().getColumnModel().getColumn(0);

        firstColumn.setPreferredWidth(-1);
        firstColumn.setMaxWidth(-1);
        firstColumn.setMinWidth(-1);
        TableColumnModel colModel = this.grid.getColumnModel();
        colModel.getColumn(0).setPreferredWidth(-1);
        colModel.getColumn(0).setMaxWidth(-1);
        colModel.getColumn(1).setPreferredWidth(140);
        colModel.getColumn(1).setWidth(140);
        colModel.getColumn(1).setMinWidth(140);
    }

    /**
     * 获取选择的ID值
     */
    private int getKeyId() {
        int row = this.grid.getSelectedRow();
        if (row >= 0) {
            return NumberUtils.convertToInteger(this.grid.getValueAt(row, 0), -1);
        }

        return -1;
    }

    /**
     * 联系人模型
     */
    public static class LinkmanTableModel extends DefaultTableModel {

        private Class[] columnType = new Class[] { String.class, String.class, String.class, String.class };

        public LinkmanTableModel() {
            super(new String[] { "id", "联系人", "手机号码", "接收短信" }, 0);

            this.refresh();
        }

        /**
         * 初始化数据
         */
        @SuppressWarnings("unchecked")
        public void refresh() {
            this.dataVector.clear();
            IDao dao = DaoFactory.INSTANCE.newDao();

            List<Linkman> list = dao.query(Linkman.class, null);
            for (Linkman info : list) {
                Vector row = new Vector();
                row.add(info.getId());
                row.add(info.getName());
                row.add(info.getPhone());
                row.add(info.getReceive());

                this.dataVector.add(row);
            }
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return false;
        }

        @Override
        public Class getColumnClass(int columnIndex) {
            return columnType[columnIndex];
        }
    }
}
